package com.pf.java.ThreadTest;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 三种创建线程的示例
 *
 * ClassName:Thread01
 * Package:com.pf.java.ThreadTest
 *
 * @author: pf
 * @date: 2021/11/21-21:53
 */
public class Thread01 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1
//        new First().start();
//        new First().start();

        // 2
//        new Thread(new Second()).start();
//        new Thread(new Second()).start();

        // 3
        FutureTask<String> task0 = new FutureTask<>(new Third());
        FutureTask<String> task1 = new FutureTask<>(new Third());
        new Thread(task0).start();
        new Thread(task1).start();


        // 主线程
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "   " + i);
        }



        // 阻塞方法
        System.out.println("返回值-0" + task0.get());
        System.out.println("返回值-1" + task1.get());
    }

    private static class First extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "   " + i);

            }
        }
    }

    private static class Second  implements  Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "   " + i);

            }
        }
    }

    private static class Third implements Callable<String> {

        @Override
        public String call() throws Exception {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "   " + i);
            }
            return Thread.currentThread().getName();
        }
    }

}
